<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Game\Platform\Doc\Tree;
use App\Models\Game\Platform\Doc\Doc;

class CreatePlatformDocTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 表已创建，跳过后续操作
        if (Tree::hasTable()) {
            return;
        }

        // 节点树
        $tableName = Tree::tableName();
        Tree::schemaConnection()->create($tableName, function (Blueprint $table) {
            $table->id('id');
            $table->foreignId('sort')->comment('排序：值越小越靠前')->nullable()->index();
            $table->foreignId('parent_id')->comment('父ID')->default(0)->index();
            $table->string('path')->comment('依赖路径')->nullable()->index();
            $table->string('name', 64)->comment('节点名称');

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');
        });
        Tree::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '接入文档--节点树'");

        // 文档
        $tableName = Doc::tableName();
        Doc::schemaConnection()->create($tableName, function (Blueprint $table) {
            $table->id('id');
            $table->foreignId('tree_id')->comment('节点ID')->index();
            $table->mediumText('md')->comment('markdown');

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');
        });
        Doc::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '接入文档--文档'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Tree::schemaConnection()->dropIfExists(Tree::tableName());
        Doc::schemaConnection()->dropIfExists(Doc::tableName());
    }
}
